﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace QuanLyThietBi
{
    class CXuly_phong
    {
        SqlConnection con;
        public CXuly_phong(SqlConnection con)
        {
            this.con = con;
        }
        public CPhong tim(string sophong)
        {
            CPhong p = null;
            con.Open();
            string str = "select * from phong where sophong=@sophong";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@sophong", sophong);
            SqlDataReader data = com.ExecuteReader();
            while (data.Read())
            {
                p = new CPhong();
                p.Sophong = data["sophong"].ToString();
                p.Tinhtrang = int.Parse(data["tinhtrang"].ToString());
                p.Loaiphong.Maloaiphong = data["maloaiphong"].ToString();
            }
            con.Close();
            con.Open();
            string strcom = "select * from thietbi where sophong=@sophong";
            SqlCommand command = new SqlCommand(strcom, con);
            command.Parameters.AddWithValue("@sophong", sophong);
            SqlDataReader datatb = command.ExecuteReader();
            while (datatb.Read())
            {
                CThietbi tb = new CThietbi();
                tb.Mathietbi = datatb["mathietbi"].ToString();
                tb.Tinhtrang = datatb["tinhtrang"].ToString();
                tb.Phieunhap.Maphieunhap = datatb["maphieunhap"].ToString();
                tb.Vattu.Mavattu = datatb["mavattu"].ToString();
                tb.Sophong = datatb["sophong"].ToString();
                p.Danhsachthietbi.Add(tb);
            }
            con.Close();
            return p;
        }
        
        public List<CPhong> getdsphong()
        {
            List<CPhong> ds = new List<CPhong>();
            con.Open();
            string str = "select * from phong";
            SqlCommand com = new SqlCommand(str, con);
            SqlDataReader data = com.ExecuteReader();
            while (data.Read())
            {
                CPhong p = new CPhong();
                p.Sophong = data["sophong"].ToString();
                p.Tinhtrang = int.Parse(data["tinhtrang"].ToString());
                p.Loaiphong.Maloaiphong = data["maloaiphong"].ToString();
                ds.Add(p);
            }
            con.Close();
            return ds;
        }
        public bool them(CPhong p)
        {
            
            if (tim(p.Sophong) != null) return false;
            bool flag = true;
            foreach (CChitietloaiphong a in p.Loaiphong.Chitietloaiphong)
            {
                int soluong = 0;
                foreach (CThietbi b in p.Danhsachthietbi)
                {
                    if (b.Vattu.Loaivattu.Maloai == a.Loaivattu.Maloai)
                    {
                        soluong++;
                    }
                }
                if (soluong < a.Soluong)
                {
                    flag = false;
                    break;
                }
            }
            if (flag ==true)
                p.Tinhtrang = 1;
            else
                p.Tinhtrang = 0;
            con.Open();
            string str = "insert into phong values(@sophong,@tinhtrang,@maloaiphong)";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@sophong", p.Sophong);
            com.Parameters.AddWithValue("@tinhtrang", p.Tinhtrang);
            com.Parameters.AddWithValue("@maloaiphong", p.Loaiphong.Maloaiphong);
            com.ExecuteNonQuery();
            con.Close();
            foreach (CThietbi tb in p.Danhsachthietbi)
            {
                con.Open();
                string strcom = "update thietbi set sophong=@sophong where mathietbi=@mathietbi";
                SqlCommand command = new SqlCommand(strcom, con);
                command.Parameters.AddWithValue("@sophong", p.Sophong);
                command.Parameters.AddWithValue("@mathietbi", tb.Mathietbi);
                command.ExecuteNonQuery();
                con.Close();
            }
          
            return true;
        }
        public void xoa(string sophong)
        {
            con.Open();
            string strcom = "update thietbi set sophong=@sophongnew where sophong=@sophong";
            SqlCommand command = new SqlCommand(strcom, con);
            command.Parameters.AddWithValue("@sophong", sophong);
            command.Parameters.AddWithValue("@sophongnew", System.DBNull.Value);
            command.ExecuteNonQuery();
            con.Close();
            con.Open();
            string str = "delete phong where sophong=@sophong";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@sophong", sophong);
            com.ExecuteNonQuery();
            con.Close();

        }
        public void sua(CPhong p)
        {
            con.Open();
            string strcom = "update thietbi set sophong=@sophongnew where sophong=@sophong";
            SqlCommand command = new SqlCommand(strcom, con);
            command.Parameters.AddWithValue("@sophong", p.Sophong);
            command.Parameters.AddWithValue("@sophongnew", System.DBNull.Value);
            command.ExecuteNonQuery();
            con.Close();
            bool flag = true;
            foreach (CChitietloaiphong a in p.Loaiphong.Chitietloaiphong)
            {
                int soluong = 0;
                foreach (CThietbi b in p.Danhsachthietbi)
                {
                    if (b.Vattu.Loaivattu.Maloai == a.Loaivattu.Maloai)
                    {
                        soluong++;
                    }
                }
                if (soluong < a.Soluong)
                {
                    flag = false;
                    break;
                }
            }
            if (flag == true)
                p.Tinhtrang = 1;
            else
                p.Tinhtrang = 0;
            con.Open();
            string str = "update phong set tinhtrang=@tinhtrang,maloaiphong=@maloaiphong where sophong=@sophong";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@sophong", p.Sophong);
            com.Parameters.AddWithValue("@tinhtrang", p.Tinhtrang);
            com.Parameters.AddWithValue("@maloaiphong", p.Loaiphong.Maloaiphong);
            com.ExecuteNonQuery();
            con.Close();
            foreach (CChitietloaiphong ct in p.Loaiphong.Chitietloaiphong)
            {
                int soluong = 1;
                foreach (CThietbi tb in p.Danhsachthietbi)
                {
                    if (tb.Vattu.Loaivattu.Maloai == ct.Maloaivattu & soluong <= ct.Soluong)
                    {
                        con.Open();
                        string s = "update thietbi set sophong=@sophong where mathietbi=@mathietbi";
                        SqlCommand c = new SqlCommand(s, con);
                        c.Parameters.AddWithValue("@sophong", p.Sophong);
                        c.Parameters.AddWithValue("@mathietbi", tb.Mathietbi);
                        c.ExecuteNonQuery();
                        con.Close();
                        soluong += 1;
                    }
                }
            }

        }
    }
}
